package han.chensing.latos.maker.windows.menus.negative.impl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import han.chensing.latos.common.level.util.StreamUtil;
import han.chensing.latos.maker.V;
import han.chensing.latos.maker.swing.FileSelector;
import han.chensing.latos.maker.windows.dialog.base.BasicDialog;
import han.chensing.latos.maker.windows.dialog.sample.SampleDialog;
import han.chensing.latos.maker.windows.menus.popup.RightClickMenu;

public class SamplesNavigationMenu extends RightClickMenu.RightClickMenuList {
    public SamplesNavigationMenu() {
        super();
        List<RightClickMenu.RightClickMenuItem> entries = getEntries();
        entries.add(new RightClickMenu.RightClickMenuItem("Import music",() -> {
            File file = FileSelector.selectFile(null);
            if (file == null || !file.exists())
                return;
            try {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                FileInputStream fileInputStream = new FileInputStream(file);
                StreamUtil.streamCopy(fileInputStream, byteArrayOutputStream);
                V.project.setMusicBytes(byteArrayOutputStream.toByteArray());
                String name = file.getName();
                int index = name.lastIndexOf('.');
                V.project.setMusicFormat(name.substring(index + 1));
                V.project.genMusic();
                fileInputStream.close();
                byteArrayOutputStream.close();
                V.container.addWindow(new BasicDialog("Nice", "Import success"));
            } catch (Exception e) {
                try {
                    String s = StreamUtil.fromException(e);
                    V.container.addWindow(new BasicDialog("Oh no", s));
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }));
        entries.add(new RightClickMenu.RightClickMenuItem("View Samples",
                () -> V.container.addWindow(new SampleDialog())));
    }
}
